﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using CodeStash.Common.DTOS;
using CodeStash.Common.Encryption;
using CodeStash.Common.Enums;

namespace RESTAPI_Test_Client
{
    internal class RESTAPISearchRunner : RESTAPIRunnerBase
    {

        internal override void RunSpecificRunnerAPILogic()
        {
            SearchByTag();
            SearchByKeyWord();
            SearchByLanguage();
  
        }


        private void SearchByTag()
        {
            DoSearch(SearchType.ByTag, "Unused_By_This_Search_But_Can_NOT_Be_Empty",1,10, CodeSnippetVisibility.JustMe,
                new string[] { "SomeTag"}, "SearchByTag");
        }


        private void SearchByKeyWord()
        {
            DoSearch(SearchType.ByKeyWord, "test", 1, 10, CodeSnippetVisibility.JustMe,
                new string[] { "" }, "SearchByKeyWord");
        }


        private void SearchByLanguage()
        {
            DoSearch(SearchType.ByLanguage, "c#", 1, 10, CodeSnippetVisibility.JustMe,
                new string[] { "" }, "SearchByLanguage");
        }



        private void DoSearch(SearchType searchType, string searchValue, int pageNo, int pageSize,
                                   CodeSnippetVisibility visibility, string[] tags, string methodName)
        {

            //REST controller Action is : Search(JSONSearchInput input)
            //Encrypt using same rules as website, see App.Config, and CodeStash Web.Config
            values = new NameValueCollection();

            JSONSearchInput input = new JSONSearchInput(
                            "", //Encrypted using same rules as website
                            base.emailForWebsite, //Encrypted using same rules as website
                            base.passwordForWebsite, //Encrypted using same rules as website
                            searchType,
                            searchValue,
                            pageNo,
                            pageSize,
                            visibility,
                            tags,
                            false,
                            0);




            Utilities.AddValue(values, "input", input);

            Byte[] results = client.UploadValues("http://localhost:8300/Rest/Search", values);

            JSONPagesSearchResultCodeSnippet searchResults = Utilities.GetValue<JSONPagesSearchResultCodeSnippet>(results);
            if (searchResults.PagedMatchedSnippets != null)
                Console.WriteLine(String.Format("{0} : Successfully searched and found total snippets of {1}, and paged results of {2}",
                    methodName, searchResults.TotalAmountOfSnippetsThatMatchedSearch, searchResults.PagedMatchedSnippets.Count()));
            else
                Console.WriteLine(searchResults.ErrorMessage);
        }






    }
}
